package com.smt.ejb3.tutoria.namedquery;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@Remote
public class StudentManager1Bean implements StudentManager1 {

	@PersistenceContext(unitName = "ejb3db")
	private EntityManager em;
	
	@Override
	public void saveStudent(Student1 student) {
		em.persist(student);
	}

	@Override
	public Student1 findStudent(String name) {
		Query query = em.createNamedQuery("findStudentByName")
			.setParameter("name", name);
		return (Student1)query.getSingleResult();
	}

	@Override
	public Student1 findStudent2(String name) {
		Query query = em.createNamedQuery("findStudentByName2")
			.setParameter(1, name);
		return (Student1)query.getSingleResult();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Student1> findAllStudents() {
		Query query = em.createNamedQuery("findAllStudents");
		return (List<Student1>)query.getResultList();
	}
	
}
